package com.nikkerball.apps.darkhorse.web.components;

import com.nikkerball.apps.darkhorse.dao.BaseDao;
import com.nikkerball.apps.darkhorse.model.base.NameObject;
import com.nikkerball.apps.darkhorse.web.session.DarkhorseSession;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.AjaxEditableLabel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * TODO: Document class
 *
 * @author marius (created on 2010-11-28)
 */
public class DarkHorseAjaxEditableLabel extends AjaxEditableLabel<String> {

    // ----- instance fields -----

    @SpringBean
    private BaseDao baseDao;

    private NameObject nameObject;

    // ----- constructors -----

    public DarkHorseAjaxEditableLabel(String id, NameObject nameObject, BaseDao baseDao) {
        super(id, new Model<String>(nameObject.getName()));
        this.nameObject = nameObject;
        this.baseDao = baseDao;
    }

    // ----------

    @Override
    protected void onSubmit(AjaxRequestTarget target) {
        String updatedName = (String) super.getDefaultModel().getObject();
        nameObject.setName(updatedName);
        String userName = ((DarkhorseSession) getSession()).getUserName();
        baseDao.update(userName, this.nameObject);
        super.onSubmit(target);
    }

}
